{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d6342cdd",
   "metadata": {},
   "source": [
    "### gym\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c3376557",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:27:17.462543Z",
     "start_time": "2024-05-08T09:27:17.458265Z"
    }
   },
   "outputs": [],
   "source": [
    "import gym"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b2f5fbfc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:27:29.326731Z",
     "start_time": "2024-05-08T09:27:24.701562Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "18e52c5c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:28:06.023435Z",
     "start_time": "2024-05-08T09:28:05.975731Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cuda.is_available()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "71a99963",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:11:50.595827Z",
     "start_time": "2024-05-08T09:11:50.585942Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.26.1'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gym.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c7af85e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:15:47.864416Z",
     "start_time": "2024-05-08T09:15:47.629203Z"
    }
   },
   "outputs": [],
   "source": [
    "#配置环境\n",
    "env=gym.make(\"CartPole-v1\",render_mode=\"rgb_array\")\n",
    "#重置环境\n",
    "env.reset()\n",
    "for i in range(10):\n",
    "    env.render() #渲染环境\n",
    "    action=env.action_space.sample() #从动作空间选一个动作\n",
    "    env.step(action) #执行动作\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1320f10f",
   "metadata": {},
   "source": [
    "### 工具类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9a2f36b1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:17:56.899186Z",
     "start_time": "2024-05-08T09:17:54.821996Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython import display\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "23e3bb1a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:21:47.445050Z",
     "start_time": "2024-05-08T09:21:47.437025Z"
    }
   },
   "outputs": [],
   "source": [
    "class GymHelper:\n",
    "    def __init__(self,env,figsize=(3,3)):\n",
    "        self.env=env\n",
    "        self.figsize=figsize\n",
    "        plt.figure(figsize=figsize)\n",
    "        self.img=plt.imshow(env.render())\n",
    "    def render(self,title=None):\n",
    "        img_data=self.env.render()\n",
    "        self.img.set_data(img_data)\n",
    "        display.display(plt.gcf())\n",
    "        display.clear_output(wait=True)\n",
    "        if title:\n",
    "            plt.title(title)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0700377d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-08T09:23:16.796307Z",
     "start_time": "2024-05-08T09:23:14.794755Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASAAAADbCAYAAADNoUzuAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy88F64QAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAVjUlEQVR4nO3db1BU97kH8O8u7C4rLjsg1z2ugGJDmzqoM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aq9WK9PT0cTXzdV49Hg8uX76M5ubmces4Z7ETVwF0h8lkingsIuPGElFlZSXef/99XLhwYdy66czZfJ3Xrq4u7N69G+fOnUNKSsqkdZyz2RdXh2CZmZlISkoa9xemt7d33F+rRFNVVYXXXnsN58+fR1ZWljGuaRoA3HPONE1DKBRCX1/fpDXzSUtLC3p7e1FQUIDk5GQkJyejsbERL774IpKTk41t5pzNvrgKIKvVioKCAni93ohxr9eLoqIiRV2pJSKorKzE2bNn8cYbbyA3NzdifW5uLjRNi5izUCiExsZGY84KCgpgsVgianp6etDe3j4v57W0tBRtbW1obW01lnXr1uEHP/gBWltbsWLFCs5ZrCg8AT4tdy7D/+Y3v5GrV69KdXW1pKamyieffKK6NSV+/OMfi9PplDfffFN6enqM5datW0ZNXV2dOJ1OOXv2rLS1tcn3v//9CS8pZ2Vlyeuvvy6XL1+WJ554IqEuKX/1KpgI5yxW4i6ARER++ctfyrJly8RqtcratWuNS86JCMCEy6lTp4yacDgshw4dEk3TxGazyYYNG6StrS3i5wwNDUllZaVkZGSI3W6XiooK6ezsjPHWqHN3AHHOYoMfx0FEysTVOSAiml8YQESkDAOIiJRhABGRMgwgIlKGAUREyjCAiEgZBhARKcMAIiJlGEBEpAwDiIiUYQARkTL/D1r+fo5gveELAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 300x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "env=gym.make(\"CartPole-v1\",render_mode=\"rgb_array\")\n",
    "#重置环境\n",
    "env.reset()\n",
    "gym_helper=GymHelper(env)\n",
    "for i in range(10):\n",
    "    gym_helper.render(title=str(i))\n",
    "    action=env.action_space.sample() #从动作空间选一个动作\n",
    "    env.step(action) #执行动作\n",
    "env.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "171.4px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
